package org.termine.jsf;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.ScheduleEntrySelectEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleModel;
import org.termine.entity.ClientProviderScheduleEvent;
import org.termine.logic.AppointmentManager;

@ManagedBean
@ViewScoped
public class ClientScheduleJSF extends BaseJSF implements Serializable {

	private static final long serialVersionUID = 6401265608725670021L;

	@ManagedProperty(value = "#{loginJSF}")
	private LoginJSF loginJSF;
	@ManagedProperty(value = "#{appointmentManager}")
	private AppointmentManager appointmentManager;

	private ScheduleModel clientScheduleModel;
	private ClientProviderScheduleEvent event;
	private Date exportStart;
	private Date exportEnd;

	/*
	 * Init
	 */
	@PostConstruct
	public void init() {
		this.event = new ClientProviderScheduleEvent();
		clientScheduleModel = new DefaultScheduleModel();
		List<ClientProviderScheduleEvent> appointments = getAppointmentManager()
				.getAllAppointmentsForClient(getLoginJSF().getPersonalData().getClient_id());
		for (int i = 0; i < appointments.size(); i++) {
			clientScheduleModel.addEvent(appointments.get(i));
		}
	}

	/*
	 * Jsf Action Methods
	 */
	public String cancelAppointment() {
		if (getAppointmentManager().cancelAppointment(event, getLoginJSF().getWhoAmI())) {
			init();
			return "SUCCESS";
		}
		return "ERROR";
	}

	public void onEventSelect(ScheduleEntrySelectEvent selectEvent) {
		event = (ClientProviderScheduleEvent) selectEvent.getScheduleEvent();
	}

	/*
	 * Getters & Setters
	 */
	public Date getExportStart() {
		return exportStart;
	}

	public void setExportStart(Date exportStart) {
		this.exportStart = exportStart;
	}

	public Date getExportEnd() {
		return exportEnd;
	}

	public void setExportEnd(Date exportEnd) {
		this.exportEnd = exportEnd;
	}

	public LoginJSF getLoginJSF() {
		return loginJSF;
	}

	public void setLoginJSF(LoginJSF loginJSF) {
		this.loginJSF = loginJSF;
	}

	public AppointmentManager getAppointmentManager() {
		return appointmentManager;
	}

	public void setAppointmentManager(AppointmentManager appointmentManager) {
		this.appointmentManager = appointmentManager;
	}

	public ScheduleModel getClientScheduleModel() {
		return clientScheduleModel;
	}

	public void setClientScheduleModel(ScheduleModel clientScheduleModel) {
		this.clientScheduleModel = clientScheduleModel;
	}

	public ClientProviderScheduleEvent getEvent() {
		return event;
	}

	public void setEvent(ClientProviderScheduleEvent event) {
		this.event = event;
	}
}
